package data;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class Room extends AbstractData {
	private long id = -1;
	private transient long hotelId;
	private String roomName;
	private RoomType roomtype;
	private String roomtypeString;
	private Floor floor;
	private String floorString;
	private Block block;
	private String blockString;
	private String comment;
	private CleaningStatus cleaningStatus;
	private String cleaningStatusString;
	private long sortOrder;
	private boolean status;
	private boolean avaliable;

	public Room() {
		status = true;
		avaliable = true;
	}

	public Room(long id) {
		this.id = id;
		this.status = true;
		this.avaliable = true;
	}

	@Override
	public long getId() {
		return id;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public long getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(long sortOrder) {
		this.sortOrder = sortOrder;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public RoomType getRoomtype() {
		if (roomtype == null && !(roomtypeString == null || roomtypeString.isEmpty())) {
			RoomType rt = new RoomType(Long.parseLong(roomtypeString));
			rt.LoadData();
			roomtype = rt;
		}
		return roomtype;
	}

	public void setRoomtype(RoomType roomtype) {
		this.roomtypeString = String.format("%d", roomtype.getId());
		this.roomtype = roomtype;
	}

	public Floor getFloor() {
		if (floor == null && !(floorString == null || floorString.isEmpty())) {
			Floor f = new Floor(Long.parseLong(floorString));
			f.LoadData();
			floor = f;
		}
		return floor;
	}

	public void setFloor(Floor floor) {
		this.floorString = String.format("%d", floor.getId());
		this.floor = floor;
	}

	
	public String getRoomtypeString() {
		return roomtypeString;
	}

	public String getFloorString() {
		return floorString;
	}

	public String getBlockString() {
		return blockString;
	}

	public String getCleaningStatusString() {
		return cleaningStatusString;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Block getBlock() {
		if (block == null && !(blockString == null || blockString.isEmpty())) {
			Block b = new Block(Long.parseLong(blockString));
			b.LoadData();
			block = b;
		}
		return block;
	}

	public void setBlock(Block block) {
		this.blockString = String.format("%d", block.getId());
		this.block = block;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public CleaningStatus getCleaningStatus() {
		if (cleaningStatus == null && !(cleaningStatusString == null || cleaningStatusString.isEmpty())) {
			CleaningStatus c = new CleaningStatus(Long.parseLong(cleaningStatusString));
			c.LoadData();
			cleaningStatus = c;
		}
		return cleaningStatus;
	}

	public void setCleaningStatus(CleaningStatus cleaningStatus) {
		this.cleaningStatusString = String.format("%d", cleaningStatus.getId());
		this.cleaningStatus = cleaningStatus;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}
	public void setRoomtypeString(String roomtypeString) {
		this.roomtypeString = roomtypeString;
	}

	public void setFloorString(String floorString) {
		this.floorString = floorString;
	}

	public void setBlockString(String blockString) {
		this.blockString = blockString;
	}

	public void setCleaningStatusString(String cleaningStatusString) {
		this.cleaningStatusString = cleaningStatusString;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.Room);
		else
			item = new Entity(this.key);

		item.setProperty("hotelId", this.hotelId);
		item.setProperty("roomName", this.roomName);
		item.setProperty("roomType", this.roomtypeString);
		item.setProperty("floor", this.floorString);
		item.setProperty("block", this.blockString);
		item.setProperty("comment", this.comment);
		item.setProperty("cleaningStatus", this.cleaningStatusString);
		item.setProperty("sortOrder", this.sortOrder);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);

		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.roomName = (String) entity.getProperty("roomName");
		this.roomtypeString = (String) entity.getProperty("roomType");
		this.floorString = (String) entity.getProperty("floor");
		this.blockString = (String) entity.getProperty("block");
		this.comment = (String) entity.getProperty("comment");
		this.cleaningStatusString = (String) entity.getProperty("cleaningStatus");
		this.sortOrder = (Long) entity.getProperty("sortOrder");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		
		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.Room, this.id);
			setEntity(entity);
		}
	}

}